package com.bolt.admin.config;

import com.bolt.admin.security.shiro.AuthContextHolder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import java.util.Optional;

/**
 * 设置审计
 */
@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class AuditingConfiguration {

    @Bean("auditorProvider")
    public AuditorAware <String> auditorProvider() {
        return () -> {
            try {
                // 这里应根据实际业务情况获取具体信息
                return Optional.of(AuthContextHolder.getAuthUserDetails().getReadName());
            } catch (Exception ignored) {
            }
            // 用户定时任务，或者无Token调用的情况
            return Optional.of("System");
        };
    }

}

 